package com.study.jyg.rxjava2study.utils.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.FragmentManager;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.utils.dialog.bottomdialog.BottomSheetDialogX;
import com.study.jyg.rxjava2study.utils.dialog.dialogFragment.BaseDialog;
import com.study.jyg.rxjava2study.utils.dialog.dialogFragment.CommonDialog;

import java.util.Objects;

public class DialogUtils {

    /**
     * 在控件下方展示的dialog
     * @param view
     * @param layout
     * @param listener
     */
    public static void menuDialog(View view, Context mContext, int layout, MenuDialogListener listener){
        Dialog dialog = new Dialog(mContext, R.style.custom_dialog2);
        View inflate = LayoutInflater.from(mContext).inflate(layout, null);
        //弹窗点击周围空白处弹出层自动消失弹窗消失(false时为点击周围空白处弹出层不自动消失)
        dialog.setCanceledOnTouchOutside(true);
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window window = dialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        //获取通知栏高度  重要的在这，获取到通知栏高度
        int notificationBar = Resources.getSystem().getDimensionPixelSize(Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));
        //获取控件 textview 的绝对坐标,( y 轴坐标是控件上部到屏幕最顶部（不包括控件本身）)
        //location [0] 为x绝对坐标;location [1] 为y绝对坐标
        int[] location = new int[2];
        view.getLocationInWindow(location); //获取在当前窗体内的绝对坐标
        view.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标
        wlp.x = 0; //对 dialog 设置 x 轴坐标
        wlp.y = location[1] + view.getHeight() - notificationBar; //对dialog设置y轴坐标
        wlp.gravity = Gravity.TOP;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
        dialog.show();//显示对话框
        if ( null != listener){
            listener.showDialog(inflate, dialog);
        }
    }
    /**
     * design库最新的Bottomdialog 底部dialog最简单实现方式
     *
     * @param mContext
     * @param layout   布局
     *                 透明样式
     */
    public static void bottomDialog(Context mContext, int layout, BottomDialogListener listener) {
        BottomSheetDialogX bottomSheetDialog = new BottomSheetDialogX(mContext);
        View contentView = View.inflate(mContext, layout, null);
        Objects.requireNonNull(bottomSheetDialog.getWindow()).setDimAmount(0.0f);
        /** 解决bottomsheetdialog 弹出不完全*/
        bottomSheetDialog.setContentView(contentView);
        View parent = (View) contentView.getParent();
        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
        contentView.measure(0, 0);
        behavior.setPeekHeight(contentView.getMeasuredHeight());
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
        params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        parent.setLayoutParams(params);
        /**  结束----------------------------*/
        //给布局设置透明背景色
        bottomSheetDialog.getDelegate().findViewById(R.id.design_bottom_sheet)
                .setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
        listener.showDialog(bottomSheetDialog);
        bottomSheetDialog.show();
    }

    /**
     * design库最新的Bottomdialog 底部dialog最简单实现方式
     *
     * @param mContext
     * @param layout   布局
     *                 透明样式
     */
    public static void bottomDialog(Context mContext, int layout, int popup, int anim, BottomDialogListener listener) {
        BottomSheetDialogX bottomSheetDialog = new BottomSheetDialogX(mContext);
        View contentView = View.inflate(mContext, layout, null);
        /** 解决bottomsheetdialog 弹出不完全*/
        bottomSheetDialog.setContentView(contentView);
        View parent = (View) contentView.getParent();
        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
        contentView.measure(0, 0);
        behavior.setPeekHeight(contentView.getMeasuredHeight());
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
        params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        parent.setLayoutParams(params);
        /**  结束----------------------------*/
        //dialog从底部弹出
        bottomSheetDialog.getWindow().setGravity(popup);
        //设置动画
        bottomSheetDialog.getWindow().setWindowAnimations(anim);
        //给布局设置透明背景色
        bottomSheetDialog.getDelegate().findViewById(R.id.design_bottom_sheet)
                .setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
        listener.showDialog(bottomSheetDialog);
        bottomSheetDialog.show();
    }

    public interface ToastDialogListener {
        void timesup();
    }

    public static void showToastDialog(FragmentManager manager, String msg, ToastDialogListener listener) {
        BaseDialog dialogs = showDialog(manager, R.layout.dialog_toast, (holder, dialog) -> {
            holder.setText(R.id.tvShow, msg);
            holder.getView(R.id.constrainLayout).setOnClickListener(v -> {
                if (null != dialog)
                    dialog.dismiss();
                if (null != listener)
                    listener.timesup();
            });
        });
        new Handler().postDelayed(() -> {
            if (dialogs != null)
                dialogs.dismiss();
            if (null != listener)
                listener.timesup();
        }, 2000);
    }

    /**
     * 通用样式dialogfragment(全部默认)
     *
     * @param manager
     */
    public static BaseDialog showDialog(FragmentManager manager, int layout, CommonDialog.ViewConvertListener listener) {
        return showDialog(manager, layout, 0.5f, false, true, listener);
    }

    /**
     * 设置左右边框
     * @param manager
     * @param layout
     * @param margin
     * @param enable
     * @param listener
     * @return
     */
    public static BaseDialog showDialog(FragmentManager manager, int layout, int margin, boolean enable, CommonDialog.ViewConvertListener listener) {
        return showDialog(manager, layout, 0.5f, false, enable, false, margin, listener);
    }

    /**
     * 通用样式dialogfragment(只设置昏暗度)
     *
     * @param manager
     */
    public static BaseDialog showDialog(FragmentManager manager, int layout, float amout, CommonDialog.ViewConvertListener listener) {
        return showDialog(manager, layout, amout, false, true, listener);
    }

    /**
     * 通用样式dialogfragment(只设置是否在底部)
     *
     * @param manager
     */
    public static BaseDialog showDialog(FragmentManager manager, boolean bottom, int layout, CommonDialog.ViewConvertListener listener) {
        return showDialog(manager, layout, 0.5f, bottom, true, listener);
    }

    /**
     * 通用样式dialogfragment(只设置外部点击)
     *
     * @param manager
     */
    public static BaseDialog showDialog(FragmentManager manager, int layout, boolean enable, CommonDialog.ViewConvertListener listener) {
        return showDialog(manager, layout, 0.5f, false, enable, listener);
    }

    /**
     * 通用样式dialogfragment(是否全屏)
     *
     * @param manager
     */
    public static BaseDialog showDialog(boolean full, FragmentManager manager, int layout, CommonDialog.ViewConvertListener listener) {
        return showDialog(manager, layout, 0.5f, false, true, full, listener);
    }

    /**
     * 通用样式dialogfragment
     *
     * @param manager
     */
    public static BaseDialog showDialog(FragmentManager manager, int layout, float amout, boolean bottom, boolean enable, CommonDialog.ViewConvertListener listener) {
        return showDialog(manager, layout, amout, bottom, enable, false, listener);
    }
    /**
     * 通用样式dialogfragment
     *
     * @param manager
     */
    public static BaseDialog showDialog(FragmentManager manager, int layout, float amout, boolean bottom, boolean enable, boolean isFull, CommonDialog.ViewConvertListener listener) {
       //全屏参数isfull 和 是否显示在底部参数bottom  在bottom 为false 时生效
        return CommonDialog.newInstance( !bottom && isFull)
                .setLayoutId(layout)
                .setConvertListener(listener)
                .setShowBottom(bottom)
                .setSize(0, 0)
                .setDimAmout(amout)//设置背景昏暗度//默认0.5f
                .setOutCancel(enable)
                .show(manager);
    }

    /**
     * 通用样式dialogfragment
     *
     * @param manager
     */
    public static BaseDialog showDialog(FragmentManager manager, int layout, float amout, boolean bottom, boolean enable, boolean isFull, int margin, CommonDialog.ViewConvertListener listener) {
        //全屏参数isfull 和 是否显示在底部参数bottom  在bottom 为false 时生效
        return CommonDialog.newInstance(!bottom && isFull)
                .setLayoutId(layout)
                .setConvertListener(listener)
                .setShowBottom(bottom)
                .setSize(0, 0)
                .setDimAmout(amout)//设置背景昏暗度//默认0.5f
                .setOutCancel(enable)
                .setMargin(margin)
                .show(manager);
    }
    /**
     * 加载数据默认的dialog
     *
     * @param manager
     * @return
     */
    public static BaseDialog showLoading(FragmentManager manager) {
        return showDialog(manager, R.layout.dialog_showgif, false, (holder, dialog) -> {
        });
    }

    public interface BottomDialogListener {
        void showDialog(BottomSheetDialog bottomSheetDialog);
    }
    public interface MenuDialogListener {
        void showDialog(View view, Dialog dialog);
    }

}
